// pages/user/friends.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists:[],
    page:1,
    lastPage:0,
    page2: 1,
    lastPage2: 0,
    tabId:0,
    lists2:[],
    loaded:0,
    loaded2:0,
    notiCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ notiCount: options.notiCount});
    if (options.notiCount>0){
      this.setData({tabId:1});
    }
    this.data.user_info = wx.getStorageSync('user_info');
    this.setData({ window: app.globalData.window })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  onPullDownRefresh: function () {
    if(this.data.tabId==0){
      this.setData({
        loading: 0,
        lastPage: 0,
        page: 1,
        lists: [],
      });
    }else{
      this.setData({
        loading2: 0,
        lastPage2: 0,
        page2: 1,
        lists2: [],
      });
    }
    
    this.getList();
  },
  indexTab: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({ tabId: id });
    if ((id == 0 && this.data.loaded == 0) || (id == 1 && this.data.loaded2 == 0)){
      this.getList();
    }
    wx.setNavigationBarTitle({
      title: id==0?'我关注的':'关注我的'
    });
  },

 

  swiperChange: function (e) {
    var id = e.detail.current;
    this.setData({ tabId: e.detail.current });
    if ((id == 0 && this.data.loaded == 0) || (id == 1 && this.data.loaded2 == 0)) {
      this.getList();
    }
    wx.setNavigationBarTitle({
      title: id == 0 ? '我关注的' : '关注我的'
    });
  },

  scrolltolower: function () {
    this.getList();
  },
  getList:function(){
    var that = this;
    var user_info = this.data.user_info;
    //我关注的
    if (this.data.tabId==0 && (this.data.loading == 1 || this.data.lastPage==1)){
      return;
    }
   //关注我的
    if (this.data.tabId == 1 && (this.data.loading2 == 1 || this.data.lastPage2 == 1)) {
      return;
    }
    if(this.data.loading){
      return ;
    }
    
    var params = {
      limit: 20,
      page: this.data.page
    };
    if (this.data.tabId == 1){
      this.data.loaded2=1;
      params.to_user_id = this.data.user_info.user_id;
      params.page = this.data.page2;
    }else{
      this.data.loaded = 1;
    }
    this.data.loading = 1;
    app.api("friends/get", params , function (res) {
      wx.stopPullDownRefresh();
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
      }
      that.delNoit("friends");
      //最后一页
      if(res.data.list.length==0){
        if(that.data.tabId==0){
          that.data.lastPage=1;
        }else{
          that.data.lastPage2 = 1;
        }
      }
      if (that.data.tabId == 0) {
        that.data.page++;
        that.setData({
          lists: that.data.lists.concat(res.data.list)
        });
      }else{
        that.data.page2++;
        that.setData({
          lists2: that.data.lists2.concat(res.data.list)
        });
      }
     
    })
  },
  //删除提醒数
  delNoit(type) {
    var that = this;
    var notiCount = this.data.notiCount;
    if (notiCount > 0 && this.data.tabId==1) {
      app.api("notification/del", { type: type }, function (res) {
        console.log(res);
        that.setData({ notiCount:0});
      })
    }

  },
  del_follow: function (e){

    var that = this;
    var idx = e.currentTarget.dataset.key;
    var id = e.currentTarget.dataset.id;

    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          //删除评论
          app.api("friends/del", {
            id: id,
            method: 'POST'
          }, function (res) {
            //删了出错
            if (res.code != 200) {
              app.alert(res.msg);
            } else {
              // wx.showToast({
              //   title: '删除成功',
              //   icon: 'success',
              //   duration: 2000
              // })
              var listData = that.data.lists;
              listData[idx]['none'] = 1;
              that.setData({ lists: listData});
            }
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  moreAct: function (e) {
    var user_id = e.currentTarget.dataset.user_id;
    var that = this;
    wx.showActionSheet({
      itemList: ['取消关注', '查看主页'],
      success: function (res) {
        if (res.tapIndex == 0) {
        console.log(e);
          that.del_follow(e);
        } else if (res.tapIndex == 1) {
          wx.navigateTo({
            url: '/pages/user/profile?id=' + user_id,
          })
        }  
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
   

})